package com.ueboot.weishop.admin.controller.customer;

import com.alibaba.fastjson.JSON;
import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.admin.controller.customer.vo.CustomerInvoiceFindReq;
import com.ueboot.weishop.admin.controller.customer.vo.CustomerInvoiceReq;
import com.ueboot.weishop.admin.controller.customer.vo.CustomerInvoiceResp;
import com.ueboot.weishop.entity.customer.CustomerInvoiceEntity;
import com.ueboot.weishop.service.customer.CustomerInvoiceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Neel on 2017/9/18.
 */
@Slf4j
@RestController
@RequestMapping(value = "/customers/invoices")
public class CustomerInvoiceController {

    @Autowired
    private CustomerInvoiceService customerInvoiceService;

    @RequiresPermissions("customers.invoices:read")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Response<Page<CustomerInvoiceResp>> page(@PageableDefault Pageable pageable, @RequestBody(required = false) CustomerInvoiceFindReq req) {
        log.info("/customers/invoices/page/");

        Page<CustomerInvoiceEntity> entities = customerInvoiceService.findBy(pageable);

        Page<CustomerInvoiceResp> body = entities.map(entity -> {
            CustomerInvoiceResp resp = new CustomerInvoiceResp();
            BeanUtils.copyProperties(entity, resp);
            resp.setDefaultInvoice(entity.getDefaultInvoice() ? "是" : "否");
            return resp;
        });

        log.info("response: {}", JSON.toJSON(body));
        return new Response<>(body);
    }


    @RequiresPermissions("customers.invoices:save")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Response<Void> save(@RequestBody CustomerInvoiceReq req) {
        log.info("/customers/invoices/save/");
        CustomerInvoiceEntity entity = null;
        if (req.getId() == null) {
            entity = new CustomerInvoiceEntity();
        } else {
            entity = customerInvoiceService.get(req.getId());
        }
        BeanUtils.copyProperties(req, entity);

        entity.setDefaultInvoice("是".equals(req.getDefaultInvoice()));

        customerInvoiceService.save(entity);

        return new Response<>();
    }


    @RequiresPermissions("customers.invoices:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response<Void> delete(Long[] id) {
        log.info("/customers/invoices/delete/{}", id);
        customerInvoiceService.delete(id);
        return new Response<>();
    }

    @RequiresPermissions("customers.invoices:delete")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Response<Void> remove(Long id) {
        log.info("/customers/invoices/remove/{}", id);
        customerInvoiceService.delete(id);
        return new Response<>();
    }


    @RequiresPermissions("customers.invoices:read")
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public Response<Void> find(@RequestBody CustomerInvoiceFindReq req) {
        log.info("/customers/invoices/find/");

        return new Response<>();
    }


}
